package com.example.carrierapp.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.carrierapp.bean.dispatch.DispatchHistoryBean;
import com.example.carrierapp.config.ConstValue;
import com.example.carrierapp.ui.dialog.DialogUtils;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.AppUtil;
import com.example.carrierapp.util.DateUtils;
import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.GlideApp;
import com.example.carrierapp.util.RoundCornerTransform;
import com.example.lb.fingerdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DispatchHistoryAdatpter extends XRecyclerView.Adapter<DispatchHistoryAdatpter.ViewHolder> {
    private List<DispatchHistoryBean.DispatchHistoryInfo> mList = new ArrayList<>();
    private int mClickIndex;
    private final RoundCornerTransform mRoundedCorners;
    private final boolean mShowopera;

    public DispatchHistoryAdatpter(Context context, boolean showOpera) {
        mShowopera = showOpera;
        mRoundedCorners = new RoundCornerTransform(DisplayUtil.dip2px(context, 4.0f));
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dispatch_history, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DispatchHistoryBean.DispatchHistoryInfo info = mList.get(position);
        holder.bindData(info, mRoundedCorners, mShowopera);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addDataNotClear(List<DispatchHistoryBean.DispatchHistoryInfo> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<DispatchHistoryBean.DispatchHistoryInfo> list) {
        if (list != null && list.size() > 0) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void updataItemData(DispatchHistoryBean.DispatchHistoryInfo bean) {
        mList.remove(mClickIndex);
        notifyItemRemoved(mClickIndex);

        mList.add(mClickIndex, bean);
        notifyItemInserted(mClickIndex);

        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public class ViewHolder extends XRecyclerView.ViewHolder {
        @BindView(R.id.iv_history_pic)
        ImageView mIvPic;
        @BindView(R.id.tv_history_name)
        TextView mTvName;
        @BindView(R.id.tv_history_phone)
        TextView mTvPhone;
        @BindView(R.id.tv_history_deadline)
        TextView mTvDeadLine;
        @BindView(R.id.tv_history_history)
        TextView mTvHistory;
        @BindView(R.id.tv_history_balance)
        TextView mTvBalance;
        @BindView(R.id.tv_history_again)
        TextView mTvAgain;
        @BindView(R.id.tv_history_modify)
        TextView mTvModify;
        @BindView(R.id.tv_dispatch_detail)
        TextView mTvDetail;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(DispatchHistoryBean.DispatchHistoryInfo info, RoundCornerTransform mRoundedCorners, boolean showOpera) {
            mTvName.setText(info.driverName + "  " + info.carNumber);
            mTvDeadLine.setText(TextUtils.isEmpty(info.validityPeriod) ? "暂无截止时间" : "截止时间：" + DateUtils.creatDay(info.validityPeriod));
            DisplayUtil.setTextOrError(mTvPhone, info.driverPhone, "暂无电话");
            mTvHistory.setText("运输记录：" + info.competedTransTimes + "次/"+info.competedTransNum+info.unitName);
            mTvBalance.setText("剩余可运：" + info.remainingTransTimes + "次/" + info.remainingTransNum + info.unitName);

            mTvModify.setVisibility(showOpera ? View.VISIBLE : View.GONE);
            mTvDetail.setVisibility(showOpera ? View.GONE : View.VISIBLE);
            if (showOpera)//待调度进来的
            {
                mTvAgain.setVisibility(!info.isMultiple ? View.VISIBLE : View.GONE);
                mTvBalance.setVisibility(!info.isMultiple?View.GONE : View.VISIBLE);
            }else//已交货进来的
            {
                mTvHistory.setText("实际运输次数：" + info.competedTransTimes + "次");
                mTvBalance.setText("实际运量：" + info.competedTransNum+ info.unitName);
            }

            GlideApp.with(itemView).load(info.carPhotoPath).placeholder(R.mipmap.icon_default_img).transform(mRoundedCorners).into(mIvPic);
            mTvPhone.setOnClickListener(v -> {
                if (TextUtils.isEmpty(mTvPhone.getText().toString().trim())) {
                    return;
                }
                new DialogUtils.Builder(itemView.getContext()).setMessage("是否拨打电话?").setPositiveButton(null, null, v1 -> AppUtil.callNumber(itemView.getContext(), ConstValue.PHONE)).creat().show();
            });

            mTvDetail.setOnClickListener(v -> {
                if (mOnBtnClickListener != null) {
                    mOnBtnClickListener.onDetailClick(info);
                }
            });

            mTvAgain.setOnClickListener(v -> {
                if (mOnBtnClickListener != null) {
                    mOnBtnClickListener.onAgainClick(info);
                }
            });

            mTvModify.setOnClickListener(v -> {
                if (mOnBtnClickListener != null) {
                    mOnBtnClickListener.onModifyClick(info);
                }
            });
            mIvPic.setOnClickListener(v -> ActivityUtil.startSeePhoto(itemView.getContext(), info.carPhotoPath));
        }
    }


    public interface OnBtnClickListener {
        void onModifyClick(DispatchHistoryBean.DispatchHistoryInfo info);

        void onAgainClick(DispatchHistoryBean.DispatchHistoryInfo info);

        void onDetailClick(DispatchHistoryBean.DispatchHistoryInfo info);
    }

    private OnBtnClickListener mOnBtnClickListener;

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        mOnBtnClickListener = listener;
    }
}
